<%-- 
    Document   : admincontentchange.jsp
    Created on : Oct 24, 2011, 8:18:30 PM
    Author     : Mr.Thanh
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@page import="java.util.*,model.dao.*,model.pojo.*,model.util.*"%>
<%@taglib prefix="box" uri="/WEB-INF/tlds/boxtag_library.tld" %>

<%
  String table=request.getParameter("page"); 
  String action=request.getParameter("action");   
  HashMap<Vector,ArrayList<String>> dsAll;
  Vector All=new Vector();
  if(table!=null )
  {      
      All=(Vector)request.getAttribute("DanhSach");
      if(All!=null&&All.size()>0){
          %>
                        
                <div class="boxDisplay">
                    <form action="administrator.html" method="post" id="frmInsertUpdate">
                      <% for(int row=0;row<All.size();row++){
                          dsAll=(HashMap<Vector,ArrayList<String>>)All.get(row);
                          if(dsAll!=null){
                          String KeyColumnId="";//cot khoa chinh can khi update
                          String KeyId=""; //khoa chinh de update
                          
                      %>
                        <table width="100%" border="0" cellspacing="1" cellpadding="1">
                          <tr class="tableheader">
                            <td align="center" class="colField"><strong>Field</strong></td>
                            <td align="center" class="colTypeKeyNull"><strong>Type</strong></td>
                             <td align="center" class="colTypeKeyNull"><strong>Null</strong></td>
                            <td align="center" class="colTypeKeyNull"><strong>Key</strong></td>
                            <td align="center" class="colValue"><strong>Value</strong></td>
                            </tr>
                         
                            <%
                             Iterator  iterator = dsAll.keySet().iterator();
                             Vector vt;
                             
                               while(iterator.hasNext()){
                                   //đặt tên cho thể input tương ứng với thứ tự dòng for
                                     String valueName="";   
                              %>
                               
                                <tr>
                                <%
                                  
                                  vt=(Vector)iterator.next();   
                                  boolean LaKhoaNgoai=false;  
                                                                                                         
                                  for(int i=0;i<vt.size();i++){
                                      if(!valueName.endsWith("PRI")){
                                            valueName=String.format("%s_%d", vt.get(0).toString(),row);
                                       }
                                      if(i<vt.size()-1){
                                          if(vt.get(i).equals("MUL")){
                                             LaKhoaNgoai=true;
                                           }else if(vt.get(i).equals("PRI")){//nếu là khóa chính
                                               KeyColumnId=vt.get(0).toString();//cột đầu tiên là khóa
                                               KeyId=vt.get(vt.size()-1).toString(); //đánh dấu để nhận bít được khóa chính khi udpate
                                               valueName+="_PRI";
                                           }
                                     %>
                                       <td align="center"><%=vt.get(i)%></td>   
                                        
                                      <%                                      
                                       }else{
                                            try{
                                                
                                          String type="";
                                          if(vt.get(1)!=null && vt.get(1).toString().indexOf("(")>0){
                                              type=vt.get(1).toString().substring(0,vt.get(1).toString().indexOf("(")).trim();
                                          }                                          
                                           //neu la khoa ngoai thi load du lieu len combobox
                                          if(LaKhoaNgoai==true){
                                                 Vector vtSelect= administratorDAO.GetDataByColumnId(vt.get(0).toString());
                                                 if(vtSelect!=null){
                                                     %>
                                                     <td align="center" name="fieldValue">
                                                         <input type="hidden" name="Field" value="<%=valueName%>_<%=type%>"/>
                                                         <select name="<%=valueName%>_<%=type%>" class="Row[<%=row%>],Field[<%=vt.get(0)%>],Type[<%=vt.get(1)%>],Null[<%=vt.get(2)%>],Key[<%=vt.get(3)%>] txtValue">
                                                            <%
                                                            for(int j=0;j<vtSelect.size();j++){
                                                            %>
                                                                  <option value="<%=vtSelect.get(j)%>"><%=vtSelect.get(j)%></option>
                                                             <%
                                                            }
                                                          %>                                           
                                                      </select>
                                                     </td>
                                                 <% }
                                            }else{
                                            
                                              
                                              
                                              
                                               String value="";
                                               if(vt.get(i)!=null){
                                                   if(vt.get(i).toString().equals("true")){
                                                       value="1";
                                                   }else if(vt.get(i).toString().equals("false")){
                                                       value="0";
                                                   }else{
                                                       value=vt.get(i).toString();
                                                   }                                                   
                                               }
                                              %>
                                              <td align="center" name="fieldValue">
                                                  <input type="hidden" name="Field" value="<%=valueName%>_<%=type%>"/>
                                                  <input name="<%=valueName%>_<%=type%>" type="text" class="Row[<%=row%>],Field[<%=vt.get(0)%>],Type[<%=vt.get(1)%>],Null[<%=vt.get(2)%>],Key[<%=vt.get(3)%>] txtValue" value="<%=value%>" />                                               
                                              </td>
                                             <% 
                                             
                                            }  
                                       
                                           }catch(Exception e){
                                                 System.out.println(e.getMessage());
                                             }                                                                                                                                      
                                      }
                                  }%>
                               </tr>
                            <%                    
                            }
                          %>
                          
                          <tr bgcolor="#F5F7FB">
                            <td colspan="5" align="right" name="Table<%=row%>">
                                <%
                                   String link="";
                                   if(action!=null && action.equals("edit")){
                                       link=String.format("page=%s&action=%s&apply=save&KeyColumnName=%s&id=%s", table,action,KeyColumnId,KeyId);
                                   }else{
                                       link=String.format("page=%s&action=%s&apply=save", table,action);
                                   }
                                   
                                   ArrayList<adminTableColumnsPOJO> colsUpload=administratorDAO.getAllColumsFromTable("vndoan",table);
                                   if(colsUpload.size()>0){
                                       %>
                                       <select name="cboFields" class="colFiels">
                                           <%
                                            for(adminTableColumnsPOJO col:colsUpload){
                                                if(col.getType().startsWith("varchar")||
                                                        col.getType().startsWith("nvarchar")||
                                                        col.getType().startsWith("text")||
                                                        col.getType().startsWith("ntext")){
                                                %>
                                                  <option value="<%=col.getField()%>" ><%=col.getField()%></option>
                                            <%  }
                                            }
                                          %>
                                       </select>
                                       <input name="FileUpload" id="FileUpload" type="file" value=""/>
                                  <%
                                   }
                                 %>               
                                   
                                   
                                    
                                    <input type="hidden" id="HiddenSave" value="<%=link%>"/>
                                    <input type="submit" id="btnSave" name="btnSave" value="Save"  />
                            </td>
                            </tr>
                          </table>    

                       
                          
                    </div>      
                          
                  </form>
                 <%}                          
                }%>    
                    
            
            </div>
       <%
      }
      
      ArrayList<String> result=(ArrayList<String>)request.getAttribute("Result");
      if(result!=null &&result.size()>0){
          %>
          <div class="boxDisplay">
          
          <table  width="100%" border="0" cellspacing="1" cellpadding="1">
              <tr class="tableheader">
                  <td align="center" style="color:#093"><strong>(<%=result.size()%>) dòng đã được xóa thành công.<strong></td>
              </tr>
              
                  <%for(String s:result){%>
                  <tr>
                  <td style="color:#900"><strong><%=ServletHelper.filter(s)%><strong></td>
                    </tr>
                   <%}%>
              
               <tr bgcolor="#F5F7FB">
               <td colspan="1" align="right"><a class="ajaxAction LienKet[page=<%=table%>&action=view]"><input type="button" name="btnTiepTuc" id="btnYes" value="Tiếp tục" /></a></td>
               
              </tr>
          </table>
        
          </div>
      <%
      }
 }
  %>
